import React from 'react';
import { connect } from 'dva';
import { message, Spin, Button } from 'antd';
import TableCop from '../components/TableCop';

class WarehousePage extends React.Component {
  init() {
    this.props.dispatch({
      type: 'warehouse/getWarehouse',
      payload: {
        success: () => {
          message.success('入库数据获取成功！');
          this.setState({ loading: false });
        },
        failed: () => {
          message.error('入库数据获取失败！');
          this.setState({ loading: true });
        },
      }
    });
  }
  constructor(props) {
    super(props);
    this.state = {
      loading: true
    }
    this.init();
  }
  render() {
    const { warehouse: { data } } = this.props;
    const columns = [
      {
        title: '单据编号',
        dataIndex: 'commodity_code',
      }, {
        title: '供应商名称',
        dataIndex: 'supplier_name',
      }, {
        title: '进货日期',
        dataIndex: 'purchase_date',
      }, {
        title: '应付金额',
        dataIndex: 'amount_payable',
      }, {
        title: '已付金额',
        dataIndex: 'amount_paid',
      }, {
        title: '备注',
        dataIndex: 'remark',
      }, {
        title: '操作',
        render: () => <div>
          <Button>edit</Button>
          <Button>delete</Button>
          <Button>detail</Button>
        </div>,
      }
    ];
    return (
      <div>
        {
          this.state.loading ?
            <Spin size="large" tip="loading ..." />
            : <TableCop rowKey="commodity_code" columns={columns} dataSource={data} />
        }
      </div>
    );
  }
}

export default connect(({ warehouse }) => ({ warehouse }))(WarehousePage);